package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.OrderInfoMapper;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.atguigu.gmall.service.RabbitService;
import com.atguigu.gmall.service.config.MqConst;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    //保存支付表   幂等性问题,可能改变了支付方式
    @Override
    public PaymentInfo insert(Long orderId, PaymentType paymentType) {
        //1、查询 是否有支付表信息
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
        .eq("order_id",orderId));
        if (null != paymentInfo){
            //修改支付方式
            paymentInfo.setPaymentType(paymentType.name());
            paymentInfoMapper.updateById(paymentInfo);
        }else {
            //保存
            paymentInfo = new PaymentInfo();
            OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
            //对外业务编号 ATGUIGU + 时间戳
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            paymentInfo.setOrderId(orderId);
            paymentInfo.setPaymentType(paymentType.name());
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            //交易内容 主题
            paymentInfo.setSubject(orderInfo.getTradeBody());
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            paymentInfo.setCreateTime(new Date());

            paymentInfoMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }

    @Override
    public void updateStatus(Map<String, String> paramMap) {
        //幂等性问题
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("out_trade_no", paramMap.get("out_trade_no")));
        if (null != paymentInfo &&
                PaymentStatus.UNPAID.name().equals(paymentInfo.getPaymentStatus())){
            //更新成已支付
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            //银行流水号
            paymentInfo.setTradeNo(paramMap.get("trade_no"));
            //交易结束时间
            paymentInfo.setCallbackTime(new Date());

            //异步回调的内容
            paymentInfo.setCallbackContent(JSONObject.toJSONString(paramMap));
            paymentInfoMapper.updateById(paymentInfo);
            System.out.println("支付表更新成功");

            //发消息MQ 更新订单状态
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                    MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());

        }
    }

    @Autowired
    private RabbitService rabbitService;

    @Override
    public PaymentInfo selectOne(String outTradeNo) {
        return paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("out_trade_no",outTradeNo));
    }
}
